#!/usr/bin/env python
# -*- coding: utf-8 -*-

import traceback
import pytest
import os
from tools.log_control import INFO
from tools.yaml_control import GetYamlData
from config.settings import ConfigHandler
from tools.feishu_control import FeiShuTalkChatBot
from config.configs import Config

def run():
    # 从配置文件中获取项目名称
    project_name = GetYamlData(ConfigHandler.config_path).get_yaml_data()['ProjectName']
    testing_evn = Config().__getattr__("CI_ENVIRONMENT_SLUG")
    try:
        try:
            DAV_ENVIRONMENT_SLUG = Config().__getattr__('DAV_ENVIRONMENT_SLUG')
            print("DAV_ENVIRONMENT_SLUG",DAV_ENVIRONMENT_SLUG)
            INFO.logger.info("""开始执行{0}项目,环境{1}...""".format(project_name, DAV_ENVIRONMENT_SLUG))
            if '-' in DAV_ENVIRONMENT_SLUG:
                DAV_ENVIRONMENT_SLUG=DAV_ENVIRONMENT_SLUG.replace("-","_")
            pytest.main(['-m %s'%DAV_ENVIRONMENT_SLUG, '-W', 'ignore:Module already imported:pytest.PytestWarning','--alluredir', './report/tmp'])
            os.system(r"allure generate ./report/tmp -o ./report/html --clean")
        except:
            INFO.logger.info("""开始执行{0}项目,默认运行{1}分支环境..""".format(project_name,testing_evn))
            pytest.main(['-m  %s'%testing_evn, '-W', 'ignore:Module already imported:pytest.PytestWarning', '--alluredir', './report/tmp'])
            os.system(r"allure generate ./report/tmp -o ./report/html --clean")

    except Exception:
        # 如有异常，相关异常发送邮件
        e = traceback.format_exc()
        # FeiShuTalkChatBot().error_feishu(e)
        raise


if __name__ == '__main__':
    run()
    # pytest.main(['-m staging'])




